import {Customer, CustomerToAdd, CustomerToUpdate} from './customer.model';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable, of} from 'rxjs';
import {tap} from 'rxjs/internal/operators';
import {Inject, Injectable} from '@angular/core';
import {AuthHttp} from '../../shared/auth.http';
import {APP_CONFIG} from '../../config/app.config';
import {WasherToAdd} from '../../washers/shared/washer.model';

@Injectable()
export class CustomerService {
  private baseUrl: string;
  private currentSelectedCustomer: Customer;

  get currentCustomer(): Customer {
    return this.currentSelectedCustomer;
  }

  set currentCustomer(currentCustomer: Customer) {
    this.currentSelectedCustomer = currentCustomer;
  }

  constructor(@Inject(APP_CONFIG) appConfig,
              private http: AuthHttp) {
    this.baseUrl = appConfig.endpoints.base;
  }

  getCustomerList(): Observable<Customer[]> {
    return this.http.get<Customer[]>(this.baseUrl + 'be_crm/v2/user/listall').pipe(
      tap( () => console.log('get user list successful...'),
        () => console.error('get user list failed...'))
    );
  }

  deleteCustomer(customerId: string): Observable<boolean> {
    console.log('inside deleteCustomer...');
    return this.http.put<boolean>(this.baseUrl + 'be_crm/v2/user/disable/' + customerId, null).pipe(
      tap(() => console.log('delete customer successful...'),
        (err) => { console.error('delete customer failed...'); console.error(err); })
    );
  }
  addCustomer(customer: CustomerToAdd): Observable<boolean> {
    console.log('inside addCustomer...');
    return this.http.post<boolean>(this.baseUrl + 'be_crm/v2/user/new', customer).pipe(
      tap( () => console.log('add user successful...'),
        () => console.error('add user failed...'))
    );
  }

  updateCustomer(customer: CustomerToUpdate): Observable<boolean> {
    console.log('inside updateCustomer...');
    // if (customer.genderId === 'M') {
    //   customer.genderId = '1';
    // } else if (customer.genderId === 'F') {
    //   customer.genderId = '2';
    // } else {
    //   customer.genderId = '0';
    // }
    return this.http.put<boolean>(this.baseUrl + 'be_crm/v2/user', customer).pipe(
      tap( () => console.log('update user successful...'),
        () => console.error('update user failed...'))
    );
  }
}
